package org.luxor.commons.zookeeper.client;

import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.ACL;
import org.luxor.commons.core.utils.Jackson2Utils;
import org.luxor.commons.core.utils.StrKit;
import org.luxor.commons.zookeeper.component.WatcherManager;
import org.luxor.commons.zookeeper.exception.ZKDataEncodingException;
import org.luxor.commons.zookeeper.exception.ZookeeperException;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/**
 * Zookeeper Client
 *
 * @author Mr.Yan  @date: 2020/12/21
 */
public class ZookeeperJsonClient extends ZookeeperWatcherClient {

    public ZookeeperJsonClient(CuratorFramework curatorFramework, WatcherManager watcherManager) {
        super(curatorFramework,watcherManager);
    }

    /**
     * 创建节点
     *
     * @param path       节点路径
     * @param createMode 节点模式
     * @param data       节点数据对象
     * @param aclList    节点访问权限，默认为"开放访问"
     */
    public void create(String path, CreateMode createMode, Object data, ArrayList<ACL> aclList) throws ZookeeperException {
        String jsonText = Jackson2Utils.toJson(data);
        try {
            byte[] dataByte = jsonText.getBytes(StrKit.CHARSET_NAME);
            this.create(path, createMode, dataByte, aclList);
        } catch (UnsupportedEncodingException e) {
            throw new ZKDataEncodingException("JSON文本解码失败", e);
        }
    }

    /**
     * 创建持久节点
     *
     * @param path 节点路径
     * @param data 节点数据对象
     */
    public void create(String path, Object data) throws ZookeeperException {
        String jsonText = Jackson2Utils.toJson(data);
        try {
            byte[] dataByte = jsonText.getBytes(StrKit.CHARSET_NAME);
            this.create(path, dataByte);
        } catch (UnsupportedEncodingException e) {
            throw new ZKDataEncodingException("JSON文本解码失败", e);
        }
    }

    /**
     * 创建临时节点
     *
     * @param path 节点路径
     * @param data 节点数据对象
     */
    public void createEphemera(String path, Object data) throws ZookeeperException {
        String jsonText = Jackson2Utils.toJson(data);
        try {
            byte[] dataByte = jsonText.getBytes(StrKit.CHARSET_NAME);
            this.createEphemera(path, dataByte);
        } catch (UnsupportedEncodingException e) {
            throw new ZKDataEncodingException("JSON文本解码失败", e);
        }
    }

    /**
     * 获取数据
     *
     * @param path  节点路径
     * @param clazz 节点数据的对象类型
     * @return 节点数据
     */
    public <T> T getDataObj(String path, Class<T> clazz) throws ZookeeperException {
        byte[] data = getData(path);
        if (data == null) {
            return null;
        }
        try {
            String jsonText = new String(data, StrKit.CHARSET_NAME);
            return Jackson2Utils.toBean(jsonText, clazz);
        } catch (UnsupportedEncodingException e) {
            throw new ZKDataEncodingException("JSON文本解码失败", e);
        }
    }


    /**
     * 修改数据
     *
     * @param path 节点路径
     * @param data 节点数据
     */
    public void setTextData(String path, Object data) throws ZookeeperException {
        String jsonText = Jackson2Utils.toJson(data);
        try {
            byte[] dataByte = jsonText.getBytes(StrKit.CHARSET_NAME);
            setData(path, dataByte);
        } catch (UnsupportedEncodingException e) {
            throw new ZKDataEncodingException("JSON文本解码失败", e);
        }
    }


}
